// CustomContextPad.js
export default class CustomContextPad {
  constructor(
    config,
    contextPad,
    create,
    elementFactory,
    injector,
    translate,
    modeling,
    bpmnFactory
  ) {
    this.create = create;
    this.elementFactory = elementFactory;
    this.translate = translate;
    this.modeling = modeling;
    this.bpmnFactory = bpmnFactory;

    if (config.autoPlace !== false) {
      this.autoPlace = injector.get("autoPlace", false);
    }

    contextPad.registerProvider(this); // 定义这是一个contextPad
  }

  getContextPadEntries(element) {
    // 删除功能编写主要通过下面步骤实现
    //将modeling引进来, 因为要使用到它的removeElements方法;
    // 定义绘制垃圾桶的功能
    // 编写className来实现修改默认样式的功能
    const {
      autoPlace,
      create,
      elementFactory,
      translate,
      modeling,
      bpmnFactory
    } = this; // modeling需要利用CustomContextPad.$inject注册进来
    function removeElement(e) {
      // 点击的时候实现删除功能
      modeling.removeElements([element]);
    }

    function clickElement(e) {
      console.log(element);
    }
    function deleteElement() {
      // 创建垃圾桶
      return {
        group: "edit",
        className: "icon-custom icon-custom-delete",
        title: translate("删除"),
        action: {
          click: removeElement
        }
      };
    }
    function editElement() {
      // 创建编辑图标
      return {
        group: "edit",
        className: "icon-custom icon-custom-edit",
        title: translate("编辑"),
        action: {
          click: clickElement
        }
      };
    }
    function appendTask(event, element) {
      if (autoPlace) {
        const shape = elementFactory.createShape({ type: "bpmn:ServiceTask" });
        autoPlace.append(element, shape); //自动连线
      } else {
        appendTaskStart(event, element);
      }
    }

    function appendTaskStart(event) {
      const shape = elementFactory.createShape({ type: "bpmn:ServiceTask" });
      create.start(event, shape, element);
    }

    return {
      "append.lindaidai-task": {
        group: "model",
        className: "icon-custom lindaidai-task",
        title: translate("创建一个类型为lindaidai-task的任务节点"),
        action: {
          click: appendTask,
          dragstart: appendTaskStart
        }
      },
      delete: deleteElement(),
      edit: editElement()
    };
  }
}

CustomContextPad.$inject = [
  "config",
  "contextPad",
  "create",
  "elementFactory",
  "injector",
  "translate",
  "modeling"
];
